contents

1. Java 멀티스레딩: 핵심 개념 및 스레드 생성

A. 멀티스레딩이란?

B. 스레드 생성 방법

1. Thread 클래스 상속

class MyThread extends Thread {
    public void run() {
        System.out.println("Thread is running");
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread t1 = new MyThread();
        t1.start(); // run()이 새로운 스레드에서 실행됨
    }
}

2. Runnable 인터페이스 구현

class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Running via Runnable");
    }
}

public class Main {
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyRunnable());
        t1.start();
    }
}

3. ExecutorService 사용 (실무 추천)

import java.util.concurrent.*;

ExecutorService executor = Executors.newFixedThreadPool(3);
for (int i = 0; i < 3; i++) {
    executor.submit(() -> System.out.println("Task executed by: " + Thread.currentThread().getName()));
}
executor.shutdown();

4. Callable & Future (결과 반환 필요할 때)

import java.util.concurrent.*;

Callable<Integer> task = () -> 123;
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Integer> future = executor.submit(task);
Integer result = future.get(); // 결과값 123 반환 (대기)
executor.shutdown();

2. 스레드 동기화 및 동시성 제어


3. 스레드 라이프사이클 및 제어


4. Spring 및 Spring Boot에서의 멀티스레딩

Spring은 비동기 프로그래밍을 더욱 쉽고 안전하게 지원하는 추상화 계층을 제공합니다.

A. @Async 활용 (비동기 실행)

@Service
public class AsyncService {
    @Async
    public void performAsyncTask() {
        System.out.println("Async task executed by: " + Thread.currentThread().getName());
    }
}
@Configuration
@EnableAsync
public class AsyncConfig {}

B. 스레드풀 구성 커스터마이징

@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(50);
        executor.setQueueCapacity(500);
        executor.initialize();
        return executor;
    }
}

C. 동기화 & 트랜잭션 안전성

D. 스케줄링 & 배치 병렬 처리


5. 멀티스레딩 베스트 프랙티스


요약 비교 표

기법 Java Spring & Spring Boot
스레드 기본 생성 Thread, Runnable, ExecutorService N/A (ExecutorService 권장)
비동기 실행 ExecutorService, Future @Async, 스케줄러, Executor
스레드풀 구성 Executors, 직접 풀 크기 조정 ThreadPoolTaskExecutor
동기화/락 synchronized, 명시적 락 synchronized, 락, 트랜잭셔널
동시성 유틸리티 CountDownLatch, Semaphore 등 모든 Java 동시성 유틸리티 활용

멀티스레딩은 빠르고 확장성 높은 Java/Spring 앱 구축의 핵심 요소입니다. 내장 추상화와 스레드풀을 적극 사용하여 병렬 처리를 구현하고, 언제나 스레드 안전과 자원 경합 문제를 최우선으로 고려해야 합니다.

references